<!--
-->
<template>
    <vxe-modal v-model="visible" :height="layout.modalHeight()" :min-width="layout.modalMinWidth(600)" :position="{top: '60px'}" :title="title" :width="layout.modalWidth(600)" destroy-on-close resize show-footer show-maximize @close="cancelClick">
        <el-form ref="ELForm" v-loading="isLoading" :model="state.model" :rules="rules" :validate-on-rule-change="false" class="ELForm" inline label-width="120px">
            <el-form-item :style="layout.col1" label="标样名称" prop="sampleName">
                <el-input v-model="state.model.sampleName" />
            </el-form-item>
            <el-form-item :style="layout.col1" label="标样类别" prop="sampleType">
                <el-select v-if="state.typeList && state.typeList.length > 0" v-model="state.model.sampleType" filterable>
                    <el-option v-for="item in state.typeList" :key="item" :label="item" :value="item" />
                </el-select>
                <el-input v-else v-model="state.model.sampleType" />
            </el-form-item>
            <el-form-item :style="layout.col1" label="基础元素" prop="elemId">
                <el-select v-model="state.model.elemId" filterable>
                    <el-option v-for="item in state.baseElemList" :key="item.id" :label="item.elemSymbol + ' ' + item.elemName" :value="item.id" />
                </el-select>
            </el-form-item>

        </el-form>
        <template #footer>
            <el-button @click="cancelClick">取消</el-button>
            <el-button :loading="isSubmitting" type="primary" @click="beforeSubmit">
                确定
            </el-button>
        </template>
    </vxe-modal>
</template>

<script setup>
import {ref, reactive, computed, onMounted} from "vue";
import {FormMixins} from "@/utils/FormMixins";
import * as api from "./api.js";

const {ELForm, visible, title, layout, isLoading, isSubmitting, state, setOptions, submitSuccess, cancelClick} = FormMixins();
const {$message} = window;

const Model = () => {
    return {}
}

// 校验规则
const rules = computed(() => {
    return {
        sampleName: [{required: true, message: "请输入标样名称"}],
        sampleType: [{required: true, message: "请输入标样类别"}],
        elemId: [{required: true, message: "请设置基础元素"}],
    }
})

// 自定义校验规则
const customRules = {
    // pid: (rule, value, callback) => {
    //     if (state.model.type > 0) {
    //         if (!value) return callback(new Error("请选择上级菜单"));
    //     }
    //     return true;
    // },
};

// 表单显示
async function show(config) {
    setOptions(config);
    state.model = Model();
    state.model = {...state.model, ...state.options}
    // 初始化数据
    api.GetBaseElemList().then(({data}) => {
        state.baseElemList = data || [];
    })
    api.GetStdSampleTypeList().then(({data}) => state.typeList = data || []);
}

// 校验表单
function beforeSubmit() {
    ELForm.value.validate(isOk => {
        isOk && submit()
    })
}

// 提交表单
async function submit() {
    isSubmitting.value = true
    console.log("submit: ", state.model)
    const body = {...state.model}
    const {success, data} = await api[state.model.id ? 'UpdateStdSample' : 'AddStdSampleInfo'](body);
    isSubmitting.value = false
    if (!success) return 0;
    $message.success("操作成功")
    submitSuccess()
}

onMounted(() => {
})
defineExpose({
    show
})
</script>


<style lang="scss" scoped>
.EditForm {

}
</style>
